package com.zyk.leetcode;

import java.util.Arrays;

/**
 * @author zhangsan
 * @date 2021/4/8 11:10
 */
public class M0301 {


    // https://leetcode-cn.com/problems/three-in-one-lcci/
    static class TripleInOne {

        private static final int STACK_NUM = 3;
        // 代表三个栈.
        int[] vs;
        int stackSize;

        public TripleInOne(int stackSize) {
            this.stackSize = stackSize;
            vs = new int[STACK_NUM * stackSize];
            Arrays.fill(vs, -1);
        }

        // 获取起始位置, o - N号桶
        public int getStart(int stackNum) {
            if(stackSize == 0) {
                throw new RuntimeException("stackSize == 0");
            }
            return stackNum * stackSize;
        }

        // 正往后添
        public void push(int stackNum, int value) {
            try {
                int start = getStart(stackNum);
                int end = start + stackSize;
                while (start < end) {
                    if(vs[start] == -1) {
                        vs[start] = value;
                        break;
                    }
                    start++;
                }
            }catch (Exception e) {
                return;
            }
        }

        // 后往前看
        public int pop(int stackNum) {
            try {
                int start = getStart(stackNum);
                int end = start + stackSize - 1;
                while (end >= start) {
                    if(vs[end] != -1) {
                        int res = vs[end];
                        vs[end] = -1;
                        return res;
                    }
                    end--;
                }
                return -1;
            }catch (Exception e) {
                return -1;
            }
        }

        public int peek(int stackNum) {
            try {
                int start = getStart(stackNum);
                int end = start + stackSize - 1;

                while (end >= start) {
                    if(vs[end] != -1) {
                        return vs[end];
                    }
                    end--;
                }
                return -1;
            }catch (Exception e) {
                return -1;
            }
        }

        public boolean isEmpty(int stackNum) {
            try {

                int start = getStart(stackNum);
                return vs[start] == -1;
            }catch (Exception e) {
                return true;
            }
        }
    }


    // for test
    public static void main(String[] args) {
        // ["TripleInOne", "isEmpty", "pop", "peek", "push", "isEmpty", "pop", "pop", "peek", "isEmpty", "pop", "isEmpty", "isEmpty", "isEmpty", "peek", "peek", "push", "push", "pop", "push", "push", "pop", "push", "pop", "peek", "isEmpty", "push", "pop", "peek", "pop", "isEmpty", "isEmpty", "pop", "peek", "isEmpty", "isEmpty"]
        //[[0], [2], [1], [0], [0, 10], [1], [0], [0], [1], [0], [2], [1], [2], [0], [2], [1], [0, 25], [2, 17], [0], [0, 12], [0, 23], [1], [2, 9], [2], [0], [2], [2, 14], [1], [0], [2], [1], [0], [1], [2], [1], [2]]
        TripleInOne tio = new TripleInOne(0);
        System.out.println(tio.isEmpty(0));
        tio.push(0, 1);
        tio.push(0, 2);
        tio.push(0, 3);

        System.out.println(tio.pop(0));
        System.out.println(tio.pop(0));
        System.out.println(tio.pop(0));
        System.out.println(tio.peek(0));

        /*TripleInOne tio = new TripleInOne(2);
        tio.push(0, 1);
        tio.push(0, 2);
        tio.push(0, 3);

        System.out.println(tio.pop(0));
        System.out.println(tio.pop(0));
        System.out.println(tio.pop(0));
        System.out.println(tio.peek(0));*/
    }

}
